package com.nondo.dean.order.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.order.domain.TransferDetail;
import com.nondo.dean.order.model.TransferDetailModelBean;
import com.nondo.dean.order.model.TransferMainBean;
import com.nondo.dean.order.model.param.TransferDetailParam;
import com.nondo.dean.order.model.result.TransferDetailResult;
import com.nondo.dean.order.service.TransferDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-06-1518:02
 * @Version
 *
 */
@RestController("ApiAdminTransferDetail")
@RequestMapping("/{v}/admin/transferDetail")
public class TransferDetailController {

    @Autowired
    TransferDetailService transferDetailService;

    /**
     * 新增
     * @param request
     * @param response
     * @param transferDetailModelBean
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<TransferDetail> createBank(HttpServletRequest request, HttpServletResponse response,
                                           @Validated @RequestBody TransferDetailModelBean transferDetailModelBean, BindingResult result) {
        View<TransferDetail> view = new View<TransferDetail>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.transferDetailService.add(transferDetailModelBean);
        }
        return view;
    }

    /**
     * 查询单个详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<TransferDetailResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<TransferDetailResult> view = new View<TransferDetailResult>(20000, "获取数据成功");

        TransferDetailResult transferDetail = null;
        if (id != null) {
            transferDetail = transferDetailService.details(id);
        } else {
            transferDetail = new TransferDetailResult();
        }

        view.setData(transferDetail);
        return view;
    }

    /**
     * 删除
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");
        TransferDetail transferDetail = transferDetailService.select(id);
        transferDetail.setDelFlag(TransferDetail.DEL_FLAG_DELETE);
        this.transferDetailService.delete(transferDetail);

        return view;
    }

    /**
     * 修改经销商打款记录
     * @param request
     * @param response
     * @param transferDetailModelBean
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<TransferDetail> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody TransferDetailModelBean transferDetailModelBean, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {

            this.transferDetailService.update(transferDetailModelBean);
        }

        return view;
    }

    /**
     * 获取列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page<TransferDetail>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<TransferDetail> query) {
        View<Page<TransferDetail>> view = new View<Page<TransferDetail>>(20000, "获取数据成功");

        TransferDetail transferDetail = query.getData();
        if(transferDetail== null){
            transferDetail=new TransferDetail();
        }

        Page<TransferDetail> page = new Page<TransferDetail>(query.getPage(), query.getLimit(), transferDetailService.count(transferDetail));

        List transferDetailsList = transferDetailService.getList(query.getOffset(), query.getLimit(), transferDetail);

        page.setContent(transferDetailsList);

        view.setData(page);

        return view;
    }


    /**
     * 查询经销商打款记录表生成凭证
     * @param request
     * @param response
     * @param param
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/search/remit")
    public View<List<TransferDetailResult>> searchRemit(HttpServletRequest request, HttpServletResponse response,@RequestBody TransferDetailParam param) {
        View<List<TransferDetailResult>> view = new View<List<TransferDetailResult>>(20000, "查询成功");
        if(null == param){
            param = new TransferDetailParam();
        }else{
              List<TransferDetailResult> transferDetailResults = transferDetailService.searchRemit(param);
              view.setData(transferDetailResults);
        }
        return view;
    }

    /**
     * 查询打印凭证
     * @param request
     * @param response
     * @param param
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/search/proof")
    public View<List<TransferDetailResult>> searchProof(HttpServletRequest request, HttpServletResponse response,@RequestBody TransferDetailParam param) {
        View<List<TransferDetailResult>> view = new View<List<TransferDetailResult>>(20000, "查询成功");
        if(null == param){
            param = new TransferDetailParam();
        }else{
            List<TransferDetailResult> transferDetailResults = transferDetailService.searchProof(param);
            view.setData(transferDetailResults);
        }
        return view;
    }

    /**
     * 订单同步
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/syn/transfer")
    public View synOrder(HttpServletRequest request,
                         HttpServletResponse response,@RequestBody TransferMainBean param){
        View<Map<String,String>> view = new View<Map<String,String>>();

        Map<String,String> maps = transferDetailService.transferPaymentRecord(param);
        if(StringUtils.isNotBlank(maps.get("MSG_ERR"))){
            view.setCode(50000);
            view.setMessage("传输失败");
        }else{
            view.setCode(20000);
            view.setMessage("传输成功");
        }
        view.setData(maps);
        return view;
    }


    /**
     * 修改经销商打款记录状态
     * @param request
     * @param response
     * @param transferDetail
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update/status")
    public View<TransferDetail> updateStauts(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody TransferDetail transferDetail, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            int num = this.transferDetailService.updateStauts(transferDetail);
            if(num <= 0){
                view.setCode(50000);
                view.setMessage("修改失败");
            }
        }
        return view;
    }
}
